//
//  SLAddDeviceVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/12.
//

#import "SLAddDeviceVC.h"
#import "SLAddLockTipVC.h"
#import "SLAddLockScanVC.h"
#import "SLAddBridgeTipVC.h"
#import "SLDeviceTypeCell.h"

static  NSString *const kCellId = @"SLDeviceTypeCell";
@interface SLAddDeviceVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation SLAddDeviceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
    [self.dataArray addObjectsFromArray:[UserManager getProductList]];
    [self loadProductList];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"Add new device");
    _tipLbl1.text = kLocalString(@"Choose a product to");
    _tipLbl2.text = kLocalString(@"Get Start!");
    [self.tabBgView addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
}

#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLDeviceTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SLProductModel *model = _dataArray[indexPath.section];
    cell.model = model;
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 20);
        headview.contentView.backgroundColor = KClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SLProductModel *model = _dataArray[indexPath.section];
    if ([model.categoryId1 isEqualToString:@"2"]) {
        SLAddBridgeTipVC *vc = [[SLAddBridgeTipVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        SLAddLockScanVC *vc = [[SLAddLockScanVC alloc] init];
        vc.type = 1;
        [self.navigationController pushViewController:vc animated:YES];
        
//        SLAddLockTipVC *vc = [[SLAddLockTipVC alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 140;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


-(void)loadProductList{
    [ZKHttpManager getWithPath:@"/msl_user/getProductList" params:@{} finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        if (!isSuccessed) {
            return;
        }
        NSArray *item = result[@"result"][@"items"];
        [self.dataArray removeAllObjects];
        [item enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SLProductModel *model = [SLProductModel modelWithDictionary:obj];
            [self->_dataArray addObject:model];
        }];
        [UserManager updateProductListWith:self->_dataArray];
        [self->_tableView reloadData];
    }];
}


@end
